////////////////////////////////////////////////////////////////////////////////////////
//
// JSimpleSim - A machine language simulator based on SimpSim by Anne-Gert Bultena.
//
// Copyright (C) 2008 Gerardo Camorlinga, Ryan Fitzgerald, and RJ Gordon
//
// This file is part of JSimpleSim.
//
// JSimpleSim is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// JSimpleSim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with JSimpleSim.  If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////////////

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsimplesim.view.components;

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

/**
 *
 * @author f08maroon
 */
public class HexCellEditor extends JTextField {

    /**
	 * 
	 */
	private static final long serialVersionUID = -893166326196626953L;
	/**
     *
     */
    protected static final String HEX = "0123456789ABCDEF";

    /**
     *
     */
    public HexCellEditor() {
        setHorizontalAlignment(CENTER);
    }

    public void setText(String text) {
        super.setText(text);
        setCaretPosition(0);
//		selectAll();
    }

    /*
     *  Override method from JComponent
     */
    public void replaceSelection(String text) {
        text = text.toUpperCase();

        // Only hex characters are allowed

        for (int i = 0; i < text.length(); i++) {
            String hexCharacter = text.substring(i, i + 1);

            if (HEX.indexOf(hexCharacter) < 0) {
                Toolkit.getDefaultToolkit().beep();
                return;
            }
        }

        //  Implement overtype mode by selecting the character at the current
        //  caret position

        int pos = getCaretPosition();

        if (getSelectedText() == null && pos < getDocument().getLength()) {
            moveCaretPosition(pos + text.length());
        }

        super.replaceSelection(text);
    }

    protected Document createDefaultModel() {
        return new HexDocument();
    }

    /**
     *
     */
    protected class HexDocument extends PlainDocument {

        /**
		 * 
		 */
		private static final long serialVersionUID = -2181001958521827143L;

		public void insertString(int offset, String text, AttributeSet as)
                throws BadLocationException {
            text = text.toUpperCase();

            // Only two characters allowed

            if (getLength() + text.length() > 2) {
                Toolkit.getDefaultToolkit().beep();
                return;
            }

            // Everythings OK, update the document

            super.insertString(offset, text, as);
        }
    }
}

